﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KouDai.Core.Helper
{
    public class ParamHelper
    {
        /// <summary>
        /// 根据键值对组装成数据字符串
        /// </summary>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static string BuildParas(Dictionary<string, object> paras)
        {
            if (paras == null || paras.Count <= 0)
            {
                throw new ArgumentException();
            }
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<string, object> param in paras)
            {
                string key = param.Key;
                object value = param.Value;
                Type type = value.GetType();
                sb.Append(key);
                sb.Append("=");
                if ("String".Equals(type.Name))
                {
                    sb.Append(value);
                }
                else if ("Int32".Equals(type.Name))
                {
                    sb.Append(Convert.ToInt32(value));
                }
                else if ("Int64".Equals(type.Name))
                {
                    sb.Append(Convert.ToInt64(value));
                }
                sb.Append("&");
            }
            return sb.ToString().Substring(0, sb.Length - 1);
        }

        /// <summary>
        /// 根据参数集合获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static T GetParam<T>(string key, string body)
        {
            if (body.Contains("?"))
            {
                body = body.Substring(body.IndexOf("?") + 1);
            }
            object value;
            Dictionary<string, object> dic = new Dictionary<string, object>();
            string[] andColl = body.Split('&');
            foreach (string str in andColl)
            {
                string[] equColl = str.Split('=');
                dic.Add(equColl[0], equColl[1]);
            }
            dic.TryGetValue(key, out value);
            return (T)value;
        }

        public static T GetParamEx<T>(string key, string body)
        {
            if (body.Contains("?"))
            {
                body = body.Substring(body.IndexOf("?") + 1);
            }
            object value;
            Dictionary<string, object> dic = new Dictionary<string, object>();
            string[] andColl = body.Split('&');
            foreach (string str in andColl)
            {
                if (str.Contains(key))
                {
                    string keyValue = str.Substring(body.IndexOf("=") + 1);
                    dic.Add(key, keyValue);
                }
            }
            dic.TryGetValue(key, out value);
            return (T)value;
        }

        /// <summary>
        /// 数据加密操作
        /// </summary>
        /// <param name="header"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public static string EncryptParam(Dictionary<string, object> body)
        {
            //Dictionary<string, object> allDic = new Dictionary<string, object>();
            //allDic.Add("header", header);
            //allDic.Add("body", body);
            ////Debug.WriteLine(JsonConvert.SerializeObject(allDic));
            //return "edata=" + HttpUtility.UrlEncode(EncryptUtil.AESEncrypt(JsonConvert.SerializeObject(allDic), "00118c9b7604620d7c73f720639de613", null));
            return "";
        }

        public static Dictionary<string, object> BuildParas(string paras)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(paras))
            {
                string[] andColl = paras.Split('&');
                foreach (string str in andColl)
                {
                    string[] equColl = str.Split('=');
                    dic.Add(equColl[0], equColl[1]);
                }
            }
            return dic;
        }
    }
}
